package com.wframe.project.jnd.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.frame.database.JdataBean;
import com.wframe.framework.database.BaseDao;

@Repository
public class AddressDao extends BaseDao{

	public List<Map> queryAddress(String userid){
		return querySqlMap("select * from wb_address where userid=? order by id", new Object[]{userid});
	}
	public void saveAddress(JdataBean bean) throws Exception{
		if(StringUtils.isNotBlank(bean.getString("id"))){
			Map key=new HashMap();
			key.put("id", bean.getString("id"));
			key.put("userid", bean.getString("userid"));
			update("wb_address", bean.getDataMap(),key);
		}else save("wb_address", bean.getDataMap());
	}
	public void delAddress(String userid,String id){
		execute("delete from wb_address where id=? and userid=?", new Object[]{userid,id});
	}
	public void setDefaultAddress(String userid,String id){
		execute("update wb_address set isdefault=0 where userid=?", new Object[]{userid});
		execute("update wb_address set isdefault=1 where userid=? and id=?", new Object[]{userid,id});
	}
	public JdataBean getCompanyAddress(String userid){
		JdataBean bean=getObject("select * from wb_company where userid=? and state=2", new Object[]{userid});
		if(bean!=null){
			JdataBean address=new JdataBean();
			address.put("getuser", bean.getString("contacts"));
			address.put("phone", bean.getString("phone"));
			address.put("shen", bean.getString("province"));
			address.put("shi", bean.getString("city"));
			address.put("qu", bean.getString("region"));
			address.put("address", bean.getString("address"));
			return address;
		}else return null;
	}
}
